package wowraidmanager.server.service.wishlist;

import java.util.ArrayList;
import java.util.Date;

import org.slim3.datastore.Datastore;

import wowraidmanager.client.service.wishlist.MemberBackupService;
import wowraidmanager.server.meta.wishlist.MemberBackupMeta;
import wowraidmanager.shared.model.wishlist.MemberBackup;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.appengine.api.datastore.Key;

public class MemberBackupServiceImpl implements MemberBackupService {

    MemberBackupMeta query = MemberBackupMeta.get();

    public void setMemberBackup(ArrayList<MemberBackup> memberBackupList){
        Datastore.put(memberBackupList);
    }

    public void deleteMemberBackup(){

        ArrayList<Key> deleteMemberBackupList = (ArrayList<Key>)Datastore.query(query).filter(query.backupDate.lessThanOrEqual(new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7))).asKeyList();

        Datastore.delete(deleteMemberBackupList);
    }

    public ArrayList<MemberBackup> getMemberBackupList(UserStatus userStatus){
        ArrayList<MemberBackup> memberBackupList = new ArrayList<MemberBackup>();

        if(userStatus.isAdmin()){
            memberBackupList = (ArrayList<MemberBackup>)Datastore.query(query).sort(query.backupDate.desc).asList();
        }else{
            memberBackupList = (ArrayList<MemberBackup>)Datastore.query(query).filter(query.guild.equal(userStatus.getMember().getGuild()), query.realm.equal(userStatus.getMember().getRealm())).sort(query.backupDate.desc).asList();
        }

        return memberBackupList;
    }
}
